/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.menosehmais.framework.util;

import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author Quintino
 */
public class MobileUtils {
    /**
     * @see http://jquerymobile.com/gbs/
     * @see http://en.wikipedia.org/wiki/Mobile_browser
     * @see http://www.useragentstring.com/pages/useragentstring.php
     */
    private static final String MOBILE_USER_AGENT_ARRAY[] = {
         "IPhone".toLowerCase()
        ,"Android".toLowerCase()
        ,"BlackBerry".toLowerCase()
        ,"Symbian".toLowerCase()
        ,"IEMobile".toLowerCase()
        ,"Mobile".toLowerCase()
        ,"Maemo".toLowerCase()
        ,"WebOS".toLowerCase()
        ,"MeeGo".toLowerCase()
        //======================================================================
        ,"Opera Mobi/".toLowerCase()
	,"Opera Mini/".toLowerCase()
	,"Fennec/".toLowerCase()
	,"Netfront/".toLowerCase()
        //======================================================================
        ," Minimo/".toLowerCase()
        ," Bada/".toLowerCase()
        //======================================================================
        //," GoBrowser".toLowerCase()
        //," Skyfire/".toLowerCase()
        //,"Blazer".toLowerCase()
        //," BOLT/".toLowerCase()
        //," Iris/".toLowerCase()
    };
    
    private static final String SMARTPHONE_USER_AGENT_ARRAY[] = {
         "IPhone".toLowerCase()
        ,"Android".toLowerCase()
        ,"BlackBerry".toLowerCase()//TODO - Criar lista completa
    };
    
    private static final String TABLET_USER_AGENT_ARRAY[] = {
         "IPad".toLowerCase()
        ,"Tablet".toLowerCase() //TODO - Criar lista completa
    };
    
    /**
     * Versão Server-Side para detectar Clientes Mobiles
     * @param request
     * @return true se for um request de um Mobile Browser
     */
    public static boolean isMobile(HttpServletRequest request) {
        String userAgent = request.getHeader("User-Agent");
        boolean ret = isSmartPhone(userAgent) || isTablet(userAgent);
        return ret;
    }
    
    public static boolean isTablet(HttpServletRequest request) {
        String userAgent = request.getHeader("User-Agent");
        boolean ret = isTablet(userAgent) && !isSmartPhone(userAgent);
        return ret;
    }
    
    public static boolean isSmartPhone(HttpServletRequest request) {
        String userAgent = request.getHeader("User-Agent");
        boolean ret = isSmartPhone(userAgent) && !isTablet(request);
        return ret;
    }
    
    private static boolean isMobile(String userAgent) {
        if(userAgent != null) {
            userAgent = userAgent.toLowerCase();
            for(String ua : MOBILE_USER_AGENT_ARRAY) {
                if( userAgent.contains(ua) ) {
                    return true;
                }
            }
        }
        return false;
    }
    
    private static boolean isTablet(String userAgent) {
        if(userAgent != null) {
            userAgent = userAgent.toLowerCase();
            for(String ua : TABLET_USER_AGENT_ARRAY) {
                if( userAgent.contains(ua) ) {
                    return true;
                }
            }
        }
        return false;
    }
    
    private static boolean isSmartPhone(String userAgent) {
        if(userAgent != null) {
            userAgent = userAgent.toLowerCase();
            for(String ua : SMARTPHONE_USER_AGENT_ARRAY) {
                if( userAgent.contains(ua) ) {
                    return true;
                }
            }
        }
        return false;
    }
}
